package com.cpit.platform.common.utils;

import org.joda.time.DateTime;
import org.joda.time.DateTimeField;
import org.joda.time.DateTimeFieldType;
import org.joda.time.format.DateTimeFormat;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Hulunliang
 * @since 2017/12/7.
 */
public class DateUtil {

    /**
     * 将对象转换为java.util.Date对象
     * @param objDate 任意对象
     * @return 成功则返回java.util.Date对象，失败则返回null
     */
    public static Date parseDate(Object objDate) {
        if (objDate == null) {
            return null;
        }
        if (objDate instanceof Date) {
            return (Date)objDate;
        }
        if (objDate instanceof Long) {
            return new Date((Long)objDate);
        }
        if (!(objDate instanceof String)) {
            return null;
        }

        String[] regEx = new String[4];
        String[] splitChars = new String[4];
        boolean result;
        Pattern pattern;
        Matcher matcher;
        String strVal = null;

        String strDate = ((String) objDate).trim();
        regEx[0] = "^(\\d{4})(年)(\\d{1,2})(月)(\\d{1,2})(日)(\\d{0,2})(时){0,1}(\\d{0,2})(分){0,1}(\\d{0,2})(秒){0,1}";
        regEx[1] = "^(\\d{4})(/)(\\d{1,2})(/)(\\d{1,2})(|)(\\d{0,2})(:){0,1}(\\d{0,2})(:){0,1}(\\d{0,2})";
        regEx[2] = "^(\\d{4})(-)(\\d{2})(-)(\\d{2})(T)(\\d{2})(:)(\\d{2})(:)(\\d{2})(\\.)\\d{3}(Z)";
        regEx[3] = "^(\\d{4})(-)(\\d{1,2})(-)(\\d{1,2})(|)(\\d{0,2})(:){0,1}(\\d{0,2})(:){0,1}(\\d{0,2})";
        splitChars[0] = "[年,月,日,时,分,秒]";
        splitChars[1] = "[/,/,|,:,:]";
        splitChars[2] = "[-,-,T,:,:,\\.,Z]";
        splitChars[3] = "[-,-,|,:,:]";
        for (int i = 0; i < 4; i++) {
            pattern = Pattern.compile(regEx[i]);
            String strTmp = strDate.replaceAll("( )+","|");
            matcher = pattern.matcher(strTmp);
            result = matcher.find();
            if (result) {
                String[] splitStrings = strTmp.split(splitChars[i]);
                strVal = getDateStringBySplitStrings(splitStrings);
                break;
            }
        }
        if (strVal == null) {
            return null;
        }
        DateTime dateTime =  DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").parseDateTime(strVal);

        return dateTime.toDate();
    }

    private static String getDateStringBySplitStrings(String[] splitStrings) {
        int year = Integer.valueOf(splitStrings[0]);
        int month = Integer.valueOf(splitStrings[1]);
        int day = Integer.valueOf(splitStrings[2]);
        int hour = 0, minute = 0, second = 0;
        if (splitStrings.length > 3 ) {
            hour = Integer.valueOf(splitStrings[3]);
        }
        if (splitStrings.length > 4 ) {
            minute = Integer.valueOf(splitStrings[4]);
        }
        if (splitStrings.length > 5 ) {
            second = Integer.valueOf(splitStrings[5]);
        }
        return String.format("%4d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second);
    }

    /**
     * 根据不含时间信息的日期得到第二天的Date对象
     * @param date 不含时间信息的日期，如2017-03-07
     * @return 第二天的Date对象
     */
    public static Date getNextDay(Date date) {
        DateTime dateTime = new DateTime(date);
        if (dateTime.get(DateTimeFieldType.hourOfDay()) == 0 && dateTime.get(DateTimeFieldType.minuteOfHour()) == 0 && dateTime.get(DateTimeFieldType.secondOfMinute()) == 0) {
            return dateTime.plusDays(1).toDate();
        }
        return date;
    }

    /**
     * 获取不含时间信息的当前日期
     * @return 不含时间信息的日期，如2017-03-07
     */
    public static Date getCurrentDate() {
        return new DateTime().withTimeAtStartOfDay().toLocalDateTime().toDate();
    }
//    public static void main(String[] args) {
//        Date date ;//= ServiceUtils.parseDate("2017年7月15日12时3分5秒");
//        //date = ServiceUtils.parseDate("2017/7/15   12:3:5");
//        date = parseDate("2017-12-07T00:00:00.000Z");
//        System.out.println(date);
//        System.out.println(getNextDay(date));
//        System.out.println(getCurrentDate());
//    }
}
